package org.frog.sgs.client;

import org.apache.mina.core.session.IoSession;
import org.frog.sgs.client.game.ClientInstance;
import org.frog.sgs.client.process.ClientProcessor;
import org.frog.sgs.client.process.ClientProcessorManager;
import org.frog.sgs.event.SgsEventCode;
import org.frog.sgs.io.SgsMsg;

/**
 * @author duanxiangqi
 * @date 2012-10-22
 */
public enum ClientMsgDispatcher {

	instance;

	public void dispatch(IoSession session, SgsMsg msg, ClientInstance instance) throws Exception {
		msg.readToBuffer();
		ClientProcessor processor = null;
		if (msg.getCode() < 1000) {
			processor = ClientProcessorManager.instance.getProcessor(SgsEventCode.SYSTEM_ALL_EVENT);
		} else {
			processor = ClientProcessorManager.instance.getProcessor(msg.getCode());
		}
		if (processor != null) {
			// System.out.println("client receive:" + msg + "," +
			// processor.getClass());
			processor.process(session, msg, instance);
		} else {
			System.err.println("无法识别的消息号：" + msg);
		}
		msg.destory();
	}
}
